package com.pat.jrgwt.shared;

import java.util.HashMap;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.pat.jrgwt.client.BaseMonthTableView;

public class FuturesTradesAmountView extends BaseMonthTableView
{
    protected HashMap<String, double[]> m_data = new HashMap<String, double[]>();

    private static class SingletonHolder
    {
        private final static FuturesTradesAmountView instance = new FuturesTradesAmountView();
    }
    
    private FuturesTradesAmountView()
    {
        m_valueFormatter = NumberFormat.getFormat("###");
        setLabel("Trades amount");
    }
    
    public static FuturesTradesAmountView getInstance()
    {
        return SingletonHolder.instance;
    }
    
    public void setData(HashMap<String, double[]> data)
    {
        m_data = data;
        // Re-define headers
        setHeaders();
    }
    
    @Override
    public void onYearChanged(int year)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void recalculate()
    {
        m_arCellValues = new double[m_arRowLabels.length][m_arHeaders.length];
        m_arCellValues = new double[][] {
                { 0, 5, 2, 0, 4, 0, 0, 0, 0, 0, 0 }
                , { 0, 2, 4, 0, 3, 13, 0, 0, 0, 0, 0 }
                , { 0, 3, 4, 0, 2, 5, 0, 0, 0, 0, 0 }
                , { 0, 2, 3, 0, 1, 2, 0, 0, 0, 0, 0 }
                , { 0, 2, 3, 0, 1, 2, 0, 0, 0, 0, 0 }
                , { 0, 27, 4, 0, 40, 5, 0, 1, 0, 0, 0 }
                , { 0, 31, 4, 0, 32, 4, 23, 1, 0, 0, 0 }
                , { 3, 23, 13, 13, 32, 10, 19, 3, 0, 0, 0 }
                , { 8, 29, 9, 8, 18, 10, 17, 5, 0, 0, 0 }
                , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
                , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
                , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
                };
    }

    @Override
    protected void setHeaders()
    {
        int total = m_data == null ? 0 : m_data.size();
        // All exchanges name + sum
        m_arHeaders = new String[total + 1];
        if( total > 0 )
        {
            int pos = 0;
            for( String key : m_data.keySet() )
            {
                m_arHeaders[pos++] = key;
            }
        }
        m_arHeaders[total] = "Sum";

        // Temporary
        m_arHeaders = new String[]{"FDAX-120", "FDAX-38", "FESX", "S&P-65", "S&P-30", "NQ", "DJ", "FGBL", "EURUSD", "YPN(Future)", "Sum"};
    }

    public static HashMap<String, double[]> parseJsonData(JavaScriptObject json)
    {
        HashMap<String, double[]> data = new HashMap<String, double[]>();
        JSONObject jsonObj = new JSONObject(json);
        double[] values = null;

        // Loop through exchange names
        for( String key : jsonObj.keySet() )
        {
            JSONArray ar = jsonObj.get(key).isArray();
            values = new double[12];
            for( int i = 0; i < ar.size(); i++ )
            {
                values[i] = Double.parseDouble(ar.get(i).toString());
            }
            data.put(key, values);
        }
        return data;
    }

}
